package com.kingdee.pda.util;

import android.content.Context;
import android.text.TextUtils;

import com.example.basecommon.utils.PreferencesUtils;
import com.kingdee.pda.MyApplication;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.plugins.http.common.OnRequestCallback;
import com.kingdee.pda.plugins.http.serverinterface.impl.WarehouseImpl;
import com.kingdee.pda.ui.activity.BaseTitleActivity;

import java.util.List;

/**
 * 仓库处理工具类
 * Created by zhanlg
 * DATE 2021/5/25
 * TIME 9:29
 */
public class WarehouseUtil {

    /**
     * 保存仓库列表
     *
     * @param warehouseList
     */
    public static void saveWarehouseList(List<Warehouse> warehouseList) {
        if (warehouseList != null && !warehouseList.isEmpty()) {
            String cacheJson = JsonUtils.GsonString(warehouseList);
            PreferencesUtils.putString(MyApplication.getInstance().getContext(), ConstantPreference.WAREHOUSE_LIST, cacheJson);
        }
    }

    /**
     * 获取缓存的仓库列表
     *
     * @return
     */
    public static List<Warehouse> getWarehouseList() {
        String cacheJson = PreferencesUtils.getString(MyApplication.getInstance().getContext(), ConstantPreference.WAREHOUSE_LIST, "");
        if (TextUtils.isEmpty(cacheJson)) {
            return null;
        }
        return JsonUtils.jsonToList(cacheJson, Warehouse.class);
    }

    /**
     * 获取线上的仓库列表
     *
     * @return
     */
    public static void getOnlineWarehouseList(Context context) {
        new WarehouseImpl().queryWarehouseList(context, true, new OnRequestCallback<List<Warehouse>>() {
            @Override
            public void onFailedCallback(boolean isRequestCallback, final String errorMsg) {
                super.onFailedCallback(isRequestCallback, errorMsg);
            }

            @Override
            public void onSuccessCallback(final List<Warehouse> result, Object object) {
                super.onSuccessCallback(result, object);
                WarehouseUtil.saveWarehouseList(result);
            }
        });
    }

    /**
     * 缓存当前仓库对象
     *
     * @param currentWarehouse
     */
    public static void saveCurrentWarehouse(Warehouse currentWarehouse) {
        if (currentWarehouse != null) {
            String cacheJson = JsonUtils.GsonString(currentWarehouse);
            PreferencesUtils.putString(MyApplication.getInstance().getContext(), ConstantPreference.CURRENT_WAREHOUSE, cacheJson);
        }
    }

    /**
     * 获取当前仓库对象
     *
     * @return
     */
    public static Warehouse getCurrentWarehouse() {
        String cacheJson = PreferencesUtils.getString(MyApplication.getInstance().getContext(), ConstantPreference.CURRENT_WAREHOUSE, "");
        if (TextUtils.isEmpty(cacheJson)) {
            return null;
        }
        return JsonUtils.GsonToBean(cacheJson, Warehouse.class);
    }

    public static void clearWarehouseInfo() {
        PreferencesUtils.putString(MyApplication.getInstance().getContext(), ConstantPreference.WAREHOUSE_LIST, "");
        PreferencesUtils.putString(MyApplication.getInstance().getContext(), ConstantPreference.CURRENT_WAREHOUSE, "");
    }
}
